package webbandidong.controller.accountcontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import webbandidong.util.Constant;

public class LogOutController extends AbstractController {

    public LogOutController() {
        
    }
    
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request, 
            HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String role = (String)session.getAttribute(Constant.USER_ROLE);
        if (session.getAttribute(Constant.USER_ID) != null) {
            session.removeAttribute(Constant.USER_ID);
        }
        if (session.getAttribute(Constant.USER_NAME) != null) {
            session.removeAttribute(Constant.USER_NAME);
        }
        if (session.getAttribute(Constant.USER_ROLE) != null) {
            session.removeAttribute(Constant.USER_ROLE);
        }
        response.sendRedirect(Constant.ADMIN_ROLE.equals(role) ? "/admin.htm" : "/index.htm");
        return null;
    }
}